package demo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

import org.yaml.snakeyaml.Yaml;

import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.JsonKit;
     
    @Path("/zheng")
    public class HelloController extends Controller {
        public void index() throws IOException {
        	 String path=getRequest().getServletContext().getRealPath("/")+"\\"+"set";
        	 //System.out.println(path);
        	 File file = new File(path);
             BufferedReader br = new BufferedReader(new FileReader(file));
             String firstLine = br.readLine();
             br.close();
             
             if(firstLine.equals("1")){//从网络读取
            	 
             }else{
            	 Yaml yaml = new Yaml();
                 String fileName = "zheng.yaml";
                 InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName);
                 
                 Map<String, Object>  kvMap = yaml.loadAs(inputStream, Map.class);
                 set("zheng",JsonKit.toJson(kvMap).toString());
             }
        	
        	 
        	 render("zheng.html");
           
        }
        
        private String readContentFromInputStream(InputStream inputStream) {
    	    StringBuilder sb = new StringBuilder();
    	    try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
    	        String line;
    	        while ((line = reader.readLine()) != null) {
    	            sb.append(line);
    	            sb.append("<br>");
    	        }
    	    } catch (IOException e) {
    	        e.printStackTrace();
    	    }
    	    return sb.toString();
    	}
    }